<?php
class Controller_biaoqian extends Controller
{
	/**
 *打印质检标签及sku产品标签
 *
 */
    public function before()
    {
    	try {
    		  ACL::Required('qc print');
    	} catch ( HTTP_Exception_403 $e ) {
				Message::error('sorry，你没有权限进这个目录');
    		$this->request->redirect ( URL::base (true) );
    	}
        parent::before();
    }

	//打印质检标签
	public function Action_biaoqian()
	{

		$arrid = trim($_GET['arrid']);
		$arrvalid = trim($_GET['arrvalid']);
		$arr_info = DB::select(array('check_quality_details.id','cid'),array('check_quality_details.check_man','cman'),'check_quality_details.*','recieve_details.*')
		->from('check_quality_details')->join('recieve_details')
		->on('check_quality_details.recieve_detail_id','=','recieve_details.id')
		->where('recieve_details.id','=',$arrvalid)
		->execute('newerp')->as_array();
		if(empty($arr_info)){
			echo "<script>alert('还没质检！请先质检！')</script>";exit;
		}
		$arr_info['0']['quality_count'] = round($arr_info['0']['qualified_quantity']/$arr_info['0']['check_quantity'],3)*100;
		$arr_info['0']['quality_count'] .= '%';
		$view=View::factory('quality/tiaoma');
		$user = array_values(qualityaction::getQualitymen(array($arr_info[0]['cman'])));
		$arr_info[0]['check_man'] = $user[0];
		$view->bind('arr_info', $arr_info);
		$this->response->body($view);
	}

	//打印SKU标签
	public function Action_skubiaoqian()
	{
		$arrid = trim($_GET['arrid']);
		$arrvalid = trim($_GET['arrvalid']);
		$arr_info = DB::select()->from('check_quality_details')->join('recieve_details')
		->on('check_quality_details.recieve_detail_id','=','recieve_details.id')
		->where('recieve_details.id','=',$arrvalid)
		->execute('newerp')->as_array();
		if(empty($arr_info)){
			echo "<script>alert('还没质检！请先质检！')</script>";exit;
		}
		$view=View::factory('quality/skubiaoqian');
		$view->bind('arr_info', $arr_info);
		$this->response->body($view);
	}
	
	//打印质检不合格标签
	public function Action_buhegebiaoqian()
	{
		$arrid = trim($_GET['arrid']);
		$arrvalid = trim($_GET['arrvalid']);
		$arr_info = DB::select(array('check_quality_details.id','cid'),array('check_quality_details.check_man','cman'),'check_quality_details.*','recieve_details.*')
		->from('check_quality_details')->join('recieve_details')
		->on('check_quality_details.recieve_detail_id','=','recieve_details.id')
		->where('recieve_details.id','=',$arrvalid)
		->execute('newerp')->as_array();
		if(empty($arr_info)){
			echo "<script>alert('还没质检！请先质检！')</script>";exit;
		}
		
		if($arr_info['0']['cid']){
			$check_detail = DB::select()->from('unqualified_deal_invoices')
			->where('check_detail_id','=',$arr_info['0']['cid'])
			->execute('newerp')->as_array();

			if(!empty($check_detail)){
				$arr_info['0']['quality_count'] = round($check_detail['0']['quantity']/$arr_info['0']['check_quantity'],3)*100;
				$arr_info['0']['quality_count'] .= '%';
				$arr_info['0']['check_quantity'] = $check_detail['0']['quantity'];
				$checkid = $check_detail['0']['id'];
			}else{
				echo "<script>alert('没找不合格质检单！')</script>";exit;
			}
		}
		$user = array_values(qualityaction::getQualitymen(array($arr_info[0]['cman'])));
		$arr_info[0]['check_man'] = $user[0];
		$view=View::factory('quality/buhege');
		$view->bind('arr_info', $arr_info);
		$view->bind('checkid', $checkid);
		$this->response->body($view);
	}

	// 打印质检报告单
	public function Action_zhijianreport()
	{
		if(!empty($_GET['id'])){

			$id = trim($_GET['id']);
			$id = substr($id,2);
			$time = date('Ymd');
			
			$where = 'id IN ('.$id.')';
			$result = qualityaction::qualityinfo($where);
			$code = 0;
			foreach ($result as $key=>$value) {

				$purchaseorder_id = qualityaction::getpurchaseid($value['purchaseorder_id']);
				//$result[$key]['check_man'] = qualityaction::getusername($purchaseorder_id);
				$currentPuOrdIds[] = $value['purchaseorder_id'];
				$result[$key]['check_man'] = Erpuser::nick_by_id($purchaseorder_id);
				if(!empty($value['pur_evidence'])){
					$code = $value['pur_evidence'];
				}
			}
			$wh = ' pur_evidence > '.$time;
			$feild = 'MAX(pur_evidence) AS maxid';
			$max = qualityaction::qualityinfo($wh,$feild);
			$max = trim($max['0']['maxid']);
			
			$evidence = '';
			if(empty($max)){
				$evidence = $time.'0001';
			}else{
				$time1 = substr($max,0,8);
				$int = substr($max,-4);
				if($time == $time1){
					$int = $int + 1;
					$len = '';
					for($i = strlen($int);$i <4 ;$i++){
						$len .= '0';
					}
					$evidence = $time.$len.$int;
				}else{
					$evidence = $time.'0001';
				}
				
			}
			if($code == 0){
				$set = 'print_status=1,pur_evidence='.$evidence;
				$table='check_quality_details';
				$where .= ' AND pur_evidence < \'1\'';
				qualityaction::updatequality($set,$where,$table);
			}
			$userId=USER::Active_user()->id;
			$user_name=Erpuser::nick_by_id($userId);
			$puOrdRemarks = !empty($currentPuOrdIds) ?
                    DB::select('id', 'remark')
                        ->from('purchaseorders')
                        ->where('id', 'in', $currentPuOrdIds)
                        ->execute('newerp')
                        ->as_array('id', 'remark') : array();
			$view=View::factory('quality/zhijian');
			$view->set('puOrdRemarks',$puOrdRemarks);
			$view->set('user_name',$user_name);
			$view->bind('evidence', $evidence);
			$view->bind('result', $result);
			$view->bind('code', $code);
			$this->response->body($view);
		}else{
			echo "<script>alert('没打印数据！');window.close()</script>";exit;
		}
	}
	// 打印质检报告单
	public function Action_zhijianreports()
	{
		$results=array();
		$j=0;
		if(!empty($_GET['id'])){
			$ids=explode('+',$_GET['id']);
			$ids=explode(" ",$_GET['id']);
			foreach ($ids as $id){
				$id = substr($id,2);
				$time = date('Ymd');
				$where = 'id IN ('.$id.')';
				$result = qualityaction::qualityinfo($where);
				$code = 0;
				foreach ($result as $key=>$value) {
	
					$purchaseorder_id = qualityaction::getpurchaseid($value['purchaseorder_id']);
					//$result[$key]['check_man'] = qualityaction::getusername($purchaseorder_id);
					$result[$key]['check_man'] = Erpuser::nick_by_id($purchaseorder_id);
					if(!empty($value['pur_evidence'])){
						$code = $value['pur_evidence'];
					}
					$currentPuOrdIds[] = $value['purchaseorder_id'];
				}
				$wh = ' pur_evidence > '.$time;
				$feild = 'MAX(pur_evidence) AS maxid';
				$max = qualityaction::qualityinfo($wh,$feild);
				$max = trim($max['0']['maxid']);
				
				$evidence = '';
				if(empty($max)){
					$evidence = $time.'0001';
				}else{
					$time1 = substr($max,0,8);
					$int = substr($max,-4);
					if($time == $time1){
						$int = $int + 1;
						$len = '';
						for($i = strlen($int);$i <4 ;$i++){
							$len .= '0';
						}
						$evidence = $time.$len.$int;
					}else{
						$evidence = $time.'0001';
					}
					
				}
				if($code == 0){
					$set = 'print_status=1,pur_evidence='.$evidence;
					$table='check_quality_details';
					$where .= ' AND pur_evidence < \'1\'';
					qualityaction::updatequality($set,$where,$table);
				}
				$userId=USER::Active_user()->id;
				$user_name=Erpuser::nick_by_id($userId);
				$results[$j]['username']=$user_name;
				$results[$j]['evidence']=$evidence;
				$results[$j]['result']=$result;
				$results[$j]['purchaseorder_id'] = $purchaseorder_id;
				if($code!=0)$results[$j]['code']=$code;
				//print_r($results[$i]);
				$j++;
			}
			$puOrdRemarks = !empty($currentPuOrdIds) ?
                    DB::select('id', 'remark')
                        ->from('purchaseorders')
                        ->where('id', 'in', $currentPuOrdIds)
                        ->execute('newerp')
                        ->as_array('id', 'remark') : array();
			//print_r($results);exit;
			$view=View::factory('quality/zhijians');
			$view->set('puOrdRemarks',$puOrdRemarks);
			$view->set('results',$results);
			$this->response->body($view);
		}else{
			echo "<script>alert('没打印数据！');window.close()</script>";exit;
		}
	}

	// 获取sku名称
	public function Action_getskuname()
	{
		if(!empty($_GET['sku'])){
			
			$skuinfo = qualityaction::getskuname($_GET['sku']);
			if(!empty($skuinfo)){
				$skuinfo['oper'] = qualityaction::geterpname($skuinfo['oper']);
				if(empty($skuinfo)){
					echo 0;
				}else{
					echo json_encode($skuinfo);
				}
			}else{
				echo 0;
			}
		}
	}

//手动新建入库单时打印sku标签
     public  function  action_print_sku(){
        $sku = strtoupper($_GET['sku']);
        $warehouseid = $_GET['warehouseid'];
    
		$arr = basic::load_sku_info($sku, $warehouseid);
		$arr_info = array();
		$arr_info[0]['sku'] = $sku; 
		$arr_info[0]['storage_position'] = $arr['products_place']; 		

		$view=View::factory('quality/skubiaoqian');
		$view->bind('arr_info', $arr_info);
		$this->response->body($view);
     }


}
